package cn.csc.module.order.api.order;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.order.api.order.dto.*;
import cn.csc.module.order.controller.admin.order.vo.OrderPageReqVO;
import cn.csc.module.order.controller.admin.order.vo.OrderWithItemRespVO;
import cn.csc.module.order.controller.app.vo.OrderRespVO;
import cn.csc.module.order.convert.addr.OrderAddrConvert;
import cn.csc.module.order.convert.item.OrderItemConvert;
import cn.csc.module.order.convert.order.OrderConvert;
import cn.csc.module.order.dal.dataobject.addr.OrderAddrDO;
import cn.csc.module.order.dal.dataobject.item.OrderItemDO;
import cn.csc.module.order.dal.dataobject.order.OrderDO;
import cn.csc.module.order.enums.ErrorCodeConstants;
import cn.csc.module.order.enums.OrderStatusEnum;
import cn.csc.module.order.service.addr.OrderAddrService;
import cn.csc.module.order.service.item.OrderItemService;
import cn.csc.module.order.service.order.OrderService;
import cn.csc.module.order.service.shopcart.ShopCartItemService;
import cn.csc.module.product.api.spu.SpuApi;
import cn.csc.module.product.api.spu.dto.SpuDTO;
import cn.csc.module.product.api.stocklock.SkuStockLockApi;
import cn.csc.module.product.api.stocklock.dto.SkuStockLockReqDTO;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.csc.framework.common.pojo.CommonResult.error;
import static cn.csc.framework.common.pojo.CommonResult.success;

/**
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/25 9:59
 * @modify
 */
@RestController
public class OrderApiImpl implements OrderApi{

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrderAddrService orderAddrService;

    @Autowired
    private ShopCartItemService shopCartItemService;

    @Override
    public CommonResult<List<OrderStatusRespDTO>> getOrdersStatus(List<BigInteger> orderIds) {
        return success(orderService.getOrdersStatus(orderIds));
    }

    @Override
    public CommonResult<PageResult<OrderRespDTO>> getOrderPage(BigInteger userId, @Valid OrderPageReqDTO pageVO) {
        OrderPageReqVO pageReqVO= OrderConvert.INSTANCE.convert01(pageVO);
        PageResult<OrderRespVO> pageResult = orderService.getMiniPage(pageReqVO);
        return success(OrderConvert.INSTANCE.convertMyPage01(pageResult));

    }

    @Override
    public CommonResult<OrderRespDTO> getOrderDetail(BigInteger orderId, BigInteger userId) {
        OrderDO orderDO=orderService.get(orderId);
        if(orderDO==null){
            return error(ErrorCodeConstants.ORDER_NOT_EXISTS);
        }
        OrderRespDTO orderRespDto = OrderConvert.INSTANCE.convertDto(orderDO);
        //查询订单项
        List<OrderItemDO> items=orderItemService.getItemByOrderId(orderId);
        orderRespDto.setOrderItems(OrderItemConvert.INSTANCE.convertDtoList(items));
        //查订单详细地址
        OrderAddrDO orderAddrDO=orderAddrService.getAddrByOrderId(orderId);
        orderRespDto.setOrderAddr(OrderAddrConvert.INSTANCE.convertDto(orderAddrDO));
        return success(orderRespDto);
    }


    /**
     * 查询订单信息，包含订单子项
     *
     * @param merchantOrderId 商户订单号
     */
    @Override
    public CommonResult<OrderWithItemRespDTO> getOrderWithItems(String merchantOrderId) {
        OrderWithItemRespVO itemRespVO = orderService.selectOrderWithItems(merchantOrderId);
        return success(OrderConvert.INSTANCE.convert(itemRespVO));
    }

    /**
     * 获取订单中的金额信息
     *
     * @param orderIds 订单id列表
     * @return 订单中的金额信息
     */
    @Override
    public CommonResult<List<OrderSimpleAmountInfoRespDTO>> getOrdersSimpleAmountInfo(List<BigInteger> orderIds) {
        return success(orderService.getOrdersSimpleAmountInfo(orderIds));
    }

    /**
     * 支付时更新订单状态
     *
     * @param orderIds 订单id列表
     * @return null
     */
    @Override
    public CommonResult<Boolean> updateOrderState(List<BigInteger> orderIds, Integer orderStatus) {
        orderService.updateOrderState(orderIds, orderStatus);
        //更新订单项的支付状态
        orderItemService.updateOrderStatus(orderIds,orderStatus);
        //更新购物车的删除状态
        shopCartItemService.deleteShopCartItemByOrderId(orderIds);

        return success(Boolean.TRUE);
    }

    @Override
    public CommonResult<String> updateReceipt(BigInteger orderId, BigInteger userId) {
        OrderDO order = orderService.getOrderByOrderId(orderId);
        if (!Objects.equals(order.getOrderStatus(), OrderStatusEnum.CONSIGNMENT.value())) {
            // 订单未发货，无法确认收货
            return CommonResult.error(ErrorCodeConstants.ORDER_NO_DELIVERY);
        }
        // 确认收货
        OrderDO orderDO=new OrderDO();
        orderDO.setId(orderId);
        orderDO.setUserId(userId);
        orderDO.setOrderStatus(OrderStatusEnum.SUCCESS.value());
        orderDO.setFinallyTime(LocalDateTime.now());
        orderService.updateOrderStatus(orderDO);
        return success("操作成功");
    }


    @Override
    public CommonResult<Long> getShopOrderNumber(BigInteger shopId) {
        return success(orderService.getCountByStatus(4,shopId));
    }

    @Override
    public CommonResult<Map<String,Object>> getCountByShopIds(List<BigInteger> shopIds){
        return success(orderService.selectCountByShopIds(shopIds));
    }


    @Override
    public CommonResult<Long> searchCartNum(BigInteger userId) {
        return success(shopCartItemService.selectNumByUserId(userId));
    }

}
